/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.controller.zxr;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.auth.LoginHandler;
import top.continew.admin.auth.LoginHandlerFactory;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.zxr.model.req.XshtbImportReq;
import top.continew.admin.zxr.model.resp.XshtbImportParseResp;
import top.continew.admin.zxr.model.resp.XshtbImportResp;
import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.zxr.model.query.XshtbQuery;
import top.continew.admin.zxr.model.req.XshtbReq;
import top.continew.admin.zxr.model.resp.XshtbDetailResp;
import top.continew.admin.zxr.model.resp.XshtbResp;
import top.continew.admin.zxr.service.XshtbService;

import java.io.IOException;

/**
 * 销售合同管理 API
 *
 * @author yfh
 * @since 2025/05/06 14:42
 */
@Tag(name = "销售合同管理 API")
@RestController
@CrudRequestMapping(value = "/zxr/xshtb", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class XshtbController extends BaseController<XshtbService, XshtbResp, XshtbDetailResp, XshtbQuery, XshtbReq> {

    @Operation(summary = "下载导入模板", description = "下载导入模板")
    @SaCheckPermission("zxr:xshtb:import")
    @GetMapping(value = "/import/template", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportTemplate(HttpServletResponse response) throws IOException {
        Long userId = UserContextHolder.getUserId();
        System.err.println("userId:"+userId);
        baseService.downloadImportTemplate(response);
    }

    @Operation(summary = "解析导入数据", description = "解析导入数据")
    @SaCheckPermission("zxr:xshtb:import")
    @PostMapping("/import/parse")
    public XshtbImportParseResp parseImport(@NotNull(message = "文件不能为空") MultipartFile file) {
        ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
        return baseService.parseImport(file);
    }

    @Operation(summary = "导入数据", description = "导入数据")
    @SaCheckPermission("zxr:xshtb:import")
    @PostMapping(value = "/import")
    public XshtbImportResp importUser(@Validated @RequestBody XshtbImportReq req) {
        return baseService.importXshtb(req);
    }

    @Operation(summary = "初审通过", description = "初审通过")
    @SaCheckPermission("zxr:xshtb:chushen")
    @GetMapping("/chushen/tg")
    public void chushenTg(Long id){
        baseService.chushenTg(id);
    }

    @Operation(summary = "初审退回", description = "初审退回")
    @SaCheckPermission("zxr:xshtb:chushen")
    @GetMapping("/chushen/th")
    public void chushenTh(Long id, String reason){
        baseService.chushenTh(id,reason);
    }

    @Operation(summary = "终审通过", description = "终审通过")
    @SaCheckPermission("zxr:xshtb:zhongshen")
    @GetMapping("/zhongshen/tg")
    public void zhongshenTg(Long id){
        baseService.zhongshenTg(id);
    }

    @Operation(summary = "终审退回", description = "终审退回")
    @SaCheckPermission("zxr:xshtb:zhongshen")
    @GetMapping("/zhongshen/th")
    public void zhongshenTh(Long id, String reason){
        baseService.zhongshenTh(id,reason);
    }
}